package dept

import "5title_gin/pkg/tree"

// SysDeptVo 视图对象（VO）用于返回前端数据
type SysDeptVo struct {
	ID           uint         `json:"id"`
	DeptName     string       `json:"dept_name"`
	DeptCategory string       `json:"dept_category"`
	Leader       uint         `json:"leader"`
	Phone        string       `json:"phone"`
	Email        string       `json:"email"`
	DeptSort     uint         `json:"dept_sort"`
	ParentId     uint         `json:"parent_id"`
	Status       uint8        `json:"status"`
	Children     []*SysDeptVo `json:"children"`
}

// GetID GetParentID SetChildren 实现 TreeNode 接口 - 注意这里使用指针接收器
func (d *SysDeptVo) GetID() uint {
	return d.ID
}

func (d *SysDeptVo) GetParentID() uint {
	return d.ParentId
}

func (d *SysDeptVo) SetChildren(children []*SysDeptVo) {
	d.Children = children
}

// GetDepartmentTree 使用树形结构工具类
func GetDepartmentTree(deptList []SysDept) []*SysDeptVo {
	// 先将 SysDept 转换为 SysDeptVo 指针切片
	deptVos := make([]*SysDeptVo, len(deptList))
	for i, dept := range deptList {
		deptVos[i] = &SysDeptVo{
			ID:           dept.ID,
			DeptName:     dept.DeptName,
			DeptCategory: dept.DeptCategory,
			Leader:       dept.Leader,
			Phone:        dept.Phone,
			Email:        dept.Email,
			DeptSort:     dept.DeptSort,
			ParentId:     dept.ParentId,
			Status:       dept.Status,
			Children:     []*SysDeptVo{},
		}
	}

	// 使用树形结构工具类构建树
	treeBuilder := tree.NewTree(deptVos)
	return treeBuilder.BuildTree(0) // 根部门的 ParentID 为 0
}
